public class Example04 {


    //迭代法
    public static int fib(int n) {
        if (n == 1) {
            return 0;
        }
        if (n == 2) {
            return 1;
        }
        int f1 = 0;
        int f2 = 1;
        int f3 = -1;
        for (int i = 3; i <= n ; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
            return f3;
        }
    public static void main(String[] args) {
        System.out.println(fib(41));
    }



    //斐波那契数列 递归
    /*public static int fib(int n) {
        if (n == 1) {
            return 0;
        }
        if (n == 2) {
            return 1;
        }
        return fib(n-1) + fib(n-2);  //递推公式
    }

    public static void main(String[] args) {
        System.out.println(fib(1));
        System.out.println(fib(2));
        System.out.println(fib(3));
        System.out.println(fib(4));
        System.out.println(fib(40));
        System.out.println(fib(41));
    }*/



   /* public static int func(int n) {
        if(n < 10) {
            return n;
        }
        int tmp = func(n / 10) + n%10;
        return tmp;
    }

    public static void main(String[] args) {
        System.out.println(func(55));
    }
*/

        //递归求1+2+......+10
    /*public static int sumN(int n) {
        if(n == 1) {
            return 1;
        }
        int tmp = n + sumN(n-1);
        return tmp;
    }

    public static void main(String[] args) {
        System.out.println(sumN(10));
    }*/

        //按顺序打出每一位数字
   /* public static void print(int n) {
        if(n < 10) {
            System.out.println(n);
            return;
        }
        print(n / 10);
        System.out.println(n % 10);
    }
    public static void main(String[] args) {
        print(123);
    }*/


    /*public static int funFac(int n) {
        if(n == 1) {
            return 1;
        }
        int tmp = n * funFac(n-1);
        return tmp;
    }

    public static void main(String[] args) {
        System.out.println(funFac(5));
    }*/


    /*public static int add(int a,int b) {
        return a+b;
    }
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(add(a, b));
    }*/


    /*public static int fac(int n){
        int ret = 1;
        for (int i = 1; i <= n; i++) {
            ret *= i;
        }
        return ret;
    }
    public static int facSum(int n){
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += fac(i);
        }
        return sum;
    }
    public static void main(String[] args) {
        System.out.println(facSum(5));
    }*/

}